package com.example.chenzhiran.myapplication7;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.chenzhiran.myapplication7.server.clientData;
import com.example.chenzhiran.myapplication7.server.serverData;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class LoginActivity extends AppCompatActivity {
    private EditText editText1;
    private EditText editText2;
    private Button lookInbutton;
    private Button forgetPasswordButton;
    clientData outdata = new clientData();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        editText1 = (EditText) findViewById(R.id.user_editText);
        editText1.getBackground().setAlpha(150);
        editText2 = (EditText) findViewById(R.id.password_editText);
        editText2.getBackground().setAlpha(150);

        lookInbutton = (Button) findViewById(R.id.look_in_button);
        lookInbutton.getBackground().setAlpha(0);
        forgetPasswordButton = (Button) findViewById(R.id.forget_password_button);
        forgetPasswordButton.getBackground().setAlpha(0);
        outdata.pageId = 1;
    }

    public void click(View v)
    {
        String inputusername = editText1.getText().toString();
        String inputpassword = editText2.getText().toString();

        switch(v.getId())
        {
            case R.id.register_button:
                if(hasWords(inputusername, inputpassword) == true) {
                    outdata.username = inputusername;
                    outdata.userpassword = inputpassword;
                    outdata.dataId = 1;
                    new connection().execute(outdata);
                }
                else return;
                break;

            case R.id.login_in_button:
                if(hasWords(inputusername, inputpassword) == true) {
                    outdata.username = inputusername;
                    outdata.userpassword = inputpassword;
                    outdata.dataId = 0;
                    new connection().execute(outdata);
                }
                else return;
                break;

            case R.id.look_in_button:
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                break;

            case R.id.forget_password_button:
                if(inputusername.length() != 0)
                {
                    outdata.dataId = 2;
                    outdata.username = inputusername;
                    new connection().execute(outdata);
                }
                break;
        }
    }

    private boolean hasWords(String inputpassword, String inputusername)
    {
        if(inputpassword == null || inputpassword.length() == 0)
        {
            Toast.makeText(LoginActivity.this, "please input password",
                    Toast.LENGTH_SHORT).show();
            return false;
        }

        if(inputusername == null || inputusername.length() == 0)
        {
            Toast.makeText(LoginActivity.this, "please input username",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    public class connection extends AsyncTask<clientData, Void, serverData>
    {
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }

        @Override
        protected serverData doInBackground(clientData... params)
        {
            clientData data = params[0];
            serverData indata = new serverData();
            try
            {
                Socket socket = new Socket("103.242.1.105", 1080);
                ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                out.writeObject(data);
                out.flush();
                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                indata = (serverData) in.readObject();
                in.close();
                out.close();
                socket.close();
            }catch (Exception e)
            {
                e.printStackTrace();
            }
            return indata;
        }

        @Override
        protected void onPostExecute(serverData data)
        {
            super.onPostExecute(data);
            if(data.datastatus == 0)
            {
                if(data.userpassword != null)
                {
                    Toast.makeText(LoginActivity.this, data.userpassword,
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.putExtra("username",outdata.username);
                startActivity(intent);
            }
            else
            {
                Toast.makeText(LoginActivity.this, data.message,
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

}

